<template>
  <el-drawer v-model="dialogVisible" title="广告效果明细" direction="rtl" size="60%">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="触发用户" align="center" min-width="100px">
        <template #default="scope">
          <el-avatar :src="scope.row.user?.avatar" /><br />
          <el-tooltip :content="'用户编号：' + scope.row.userId" placement="top">
            <el-text>{{ scope.row.advertiser || scope.row.user?.nickname }}</el-text>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="类型及场景" align="left" min-width="180">
        <template #default="{ row }">
          <el-tag type="primary">{{ row.typeName }}</el-tag>
          <span class="ml-10px">{{ row.sceneName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="花费金额" align="center" min-width="150">
        <template #default="{ row }">
          <span class="text-red-500">{{ fenToYuan(row.spendPrice) }} 元</span>
        </template>
      </el-table-column>
      <el-table-column
        :formatter="dateFormatter"
        align="center"
        label="发生时间"
        prop="createTime"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as AdEffectApi from '@/api/ad/effect'
import { fenToYuan } from '@/utils'

defineOptions({ name: 'AdEffectRecord' })

const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  planId: undefined
})
const list = ref<any>([]) // 列表的数据
/** 查询列表 */
const getList = async (planId: number | string) => {
  queryParams.planId = planId
  try {
    loading.value = true
    const data = await AdEffectApi.getPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const open = async (planId: number | string) => {
  dialogVisible.value = true
  await getList(planId)
}
defineExpose({ open })
</script>
<style lang="scss" scoped></style>
